﻿using System;
using System.Data;
using System.ComponentModel;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System.Windows.Forms;
using System.Linq;
using System.Collections.Generic;
using Mota.EditModule;

namespace Mota.XMLControls
{

    /// <summary>
    /// Represent CheckedList based on DX CheckedListBoxControl
    /// </summary>
    public class DXGuiCheckedList:GuiCheckedList
    {
        /// <summary>
        /// Create CheckedListBoxControl 
        /// and set its CheckOnClick property to true
        /// </summary>
        public DXGuiCheckedList()
        {
            visualControl = new MyDxCheckedListBox();
            (visualControl as MyDxCheckedListBox).CheckOnClick = true;
        }

        /// <summary>
        /// GET|SET CheckedListBoxControl Text property
        /// </summary>
        public override string Text
        {
            get
            {
                return (visualControl as MyDxCheckedListBox).Text;
            }
            set
            {
                (visualControl as MyDxCheckedListBox).Text = value;
            }
        }

        /// <summary>
        /// SET| GET CheckedListBoxControl DataSource property
        /// </summary>
        public override object Binding
        {
            get
            {
                return (visualControl as MyDxCheckedListBox).DataSource;
            }
            set
            {
                if (value is BindingSource)
                {
                    var vc = (visualControl as MyDxCheckedListBox);
                    vc.BeginUpdate();
                    try
                    {
                        
                        vc.DataSource = value;
                        vc.ValueMember = XmlAttributes["_KeyField"];
                        vc.DisplayMember = XmlAttributes["_Fields"].Split(',').Last();
                    }
                    finally
                    {
                        vc.EndUpdate();
                    }
                }
            }
        }
        /// <summary>
        /// Set Control to current main BindingSource value
        /// </summary>
        /// <param name="valueBinding">main BindingSource with current record values</param>
        public override void SetValue(object valueBinding)
        {
            if ((valueBinding is BindingSource) && ((visualControl as MyDxCheckedListBox).DataBindings["XmlItems"] == null))
                (visualControl as MyDxCheckedListBox).DataBindings.Add("XmlItems", valueBinding as BindingSource, XmlAttributes["XMLAttribute"], true);
            //if ((visualControl as CheckedListBoxControl).DataSource != null)
            //{
            //    CheckedListBoxControl checkListBoxControl = (visualControl as CheckedListBoxControl);
            //    // store valueBinding for next use
            //    checkListBoxControl.Tag = valueBinding;
            //    // get currecy manager
            //    CurrencyManager cm = (valueBinding as BindingSource).CurrencyManager;
            //    // get propertydescriptor
            //    PropertyDescriptor pdValue = cm.GetItemProperties()[XmlAttributes["XMLAttribute"]];
            //    if (pdValue != null)
            //    {
            //        // TODO: modify checking logic
            //        String currValues = pdValue.GetValue(cm.Current).ToString();
            //        // parse values separated by semicolon 
            //        String[] valuesArray = currValues.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            //        if (valuesArray.Length > 0)
            //        {
            //            int i = 0;
            //            object item = checkListBoxControl.GetItem(i);
            //            while (item!=null)
            //            {
            //                // check if cur item value equal any in valuesArray and ignorecase
            //                if(valuesArray.Any<String>(
            //                    u => u.Equals(checkListBoxControl.GetItemValue(i).ToString(),
            //                                    StringComparison.OrdinalIgnoreCase))
            //                    )
            //                {
            //                    // check item if found
            //                        checkListBoxControl.SetItemChecked(i, true);
            //                }
            //                // increase i and get next element
            //                item = checkListBoxControl.GetItem(++i);
            //            }
            //        }

            //        checkListBoxControl.ItemCheck += CheckedListBox1_ItemCheck;
            //    }
            //}
        }

        public override void Refresh(ConnectionConfigs configs, BindingSource mainBs)
        {
            this.Rfsh(mainControls, mainBs, configs);
        }

        public override object GetValue()
        {
            return (visualControl as MyDxCheckedListBox).XmlItems;
        }
    }
}
    